/**
 * Created by HuyNguyen on 6/12/14.
 */
var Change_Password = function (){
    var languages = {},
        util = new UTIL();
    var changePasswordUrl = "user/changePassword";

    var handleChangePassword = function(){

        $("#change-password-form").bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            submitButtons: 'button[type="submit"]',
            fields: {
                'ChangePasswordForm[currentPassword]': {
                    validators: {
                        notEmpty: {
                            message: util.Format(languages.culture().text.require_check,languages.culture().text.password)
                        },
                        different: {
                            field : 'ChangePasswordForm[newPassword]',
                            message: util.Format(languages.culture().text.different_check, languages.culture().text.password, languages.culture().text.new_password)
                        }
                    }
                },
                'ChangePasswordForm[newPassword]': {
                    validators: {
                        notEmpty: {
                            message: util.Format(languages.culture().text.require_check,languages.culture().text.new_password)
                        },
                        identical: {
                            field: 'ChangePasswordForm[retypeNewPassword]',
                            message: util.Format(languages.culture().text.confirm_check,languages.culture().text.new_password, languages.culture().text.retype_new_password)
                        },
                        different: {
                            field : 'ChangePasswordForm[currentPassword]',
                            message: util.Format(languages.culture().text.different_check, languages.culture().text.password, languages.culture().text.new_password)
                        }
                    }
                },
                'ChangePasswordForm[retypeNewPassword]': {
                    validators: {
                        notEmpty: {
                            message: util.Format(languages.culture().text.require_check,languages.culture().text.retype_new_password)
                        },
                        identical: {
                            field: 'ChangePasswordForm[newPassword]',
                            message: util.Format(languages.culture().text.confirm_check,languages.culture().text.new_password, languages.culture().text.password)
                        }

                    }
                }
            }
        }).on('success.form.bv', function(e) {
           // e.preventDefault();
            $("#change-password-form button[type=submit]").prop("disabled","disabled");
            $.ajax({
                url : changePasswordUrl,
                data :  $("#change-password-form").serializeArray(),
                dataType : "json",
                type : "POST",
                success : function(returnData){
                   if(returnData.resultCd == '00'){
                       toastr.options = {
                           "closeButton": true,
                           "debug": false,
                           "positionClass": "toast-top-right",
                           "onclick": null,
                           "showDuration": "600",
                           "hideDuration": "1000",
                           "timeOut": "5000",
                           "extendedTimeOut": "1000",
                           "showEasing": "swing",
                           "hideEasing": "linear",
                           "showMethod": "slideDown",
                           "hideMethod": "slideUp"
                       }
                       toastr['success'](languages.culture().text.change_password);
                       $('.password-dialog').modal('hide');
                   }else{
                       alert(returnData.msg);
                   }
                }
            });
            return false;
        }).on('error.validator.bv', function(e, data) {
            // $(e.target)    --> The field element
            // data.bv        --> The BootstrapValidator instance
            // data.field     --> The field name
            // data.element   --> The field element
            // data.validator --> The current validator name

            data.element
                .data('bv.messages')
                // Hide all the messages
                .find('.help-block[data-bv-for="' + data.field + '"]').hide()
                // Show only message associated with current validator
                .filter('[data-bv-validator="' + data.validator + '"]').show();
        });
    };

    return {
        init: function(language){
            languages = new Language(language);
            handleChangePassword();
        }
    }

}()
